const AC_GAME_OBJEXTS = [];

export class AcGameObject{
    constructor (){
        AC_GAME_OBJEXTS.push(this);
        this.timedelta = 0;//这一帧与上一帧的执行间隔
        this.has_called_start = false;
    }

    start(){//只执行一次

    }

    update(){//出第一帧之外每一帧都执行

    }

    on_destroy(){   //删除之前执行

    }

    destroy(){

        this.on_destroy();
        for (let i in AC_GAME_OBJEXTS){
            const obj = AC_GAME_OBJEXTS[i];
            if (obj == this){
                AC_GAME_OBJEXTS.splice(i);
                break;
            }
        }
    }
}

let last_timestamp;//上一次执行的时刻
const step = (timestamp) => {
    for (let obj of AC_GAME_OBJEXTS){
        if (!obj.has_called_start){
            obj.has_called_start = true;
            obj.start();
        } else {
            obj.timedelta = timestamp - last_timestamp;
            obj.update();
        }
    }

    last_timestamp = timestamp;
    requestAnimationFrame(step);
}

requestAnimationFrame(step);